package com.besttone.search;

import java.util.ArrayList;
import java.util.List;

import com.besttone.widget.AlphabetBar;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.besttone.adapter.CityListAdapter;
import com.besttone.search.model.City;
import com.besttone.search.sql.NativeDBHelper;
import com.besttone.search.util.LogUtils;
import com.besttone.search.util.SharedUtils;

public class SelectCityActivity extends CityListBaseActivity {
	private ImageButton btn_back;
	private Context mContext;
	private NativeDBHelper mDB;
	
	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		//this.imm = ((InputMethodManager)getSystemService("input_method"));
		btn_back = (ImageButton) findViewById(R.id.left_title_button);
		btn_back.setOnClickListener(backListner);

	}

	private ImageButton.OnClickListener backListner = new ImageButton.OnClickListener() {
		public void onClick(View v) {
			finish();
		}
	};

	private void selectComplete(City paramCity) {
		String simplifyCode = "";
		if(paramCity!=null && paramCity.getCityCode()!=null) {
			simplifyCode = paramCity.getCityCode();
			if(simplifyCode.endsWith("00")) {
				simplifyCode = simplifyCode.substring(0, simplifyCode.length()-2);
			}
			if(simplifyCode.endsWith("00")) {
				simplifyCode = simplifyCode.substring(0, simplifyCode.length()-2);
			}
		}
		paramCity.setSimplifyCode(simplifyCode);
		SharedUtils.setCurrentCity(this.mContext, paramCity);
		Intent localIntent = getIntent();
		localIntent.putExtra("cityName", paramCity.getCityName());
		setResult(RESULT_OK, localIntent);
		finish();
	}

	@Override
	public void addCityFirstLetter() {
		if (this.mCityFirstLetter.length <= 0)
			return;
		String str = null;
		Cursor localCursor = null;
		for (int i = 1; i < this.mCityFirstLetter.length; i++) {
			str = this.mCityFirstLetter[i];
			String[] arrayParm = new String[2];
			arrayParm[0] = "2";
			arrayParm[1] = (str + "%");
			localCursor = this.mDB.select(null, "TYPE=? AND FIRST_PY like ?",
					arrayParm);
			if (localCursor != null)
				if (localCursor.moveToNext()) {
					City localCity1 = new City();
					localCity1.setCityName("-1");
					localCity1.setFirstLetter(str);
					this.citylist.add(localCity1);
				}

			while (true) {
				if (localCursor.isAfterLast()) {
					localCursor.close();
					break;
				}
				City localCity2 = new City();
				localCity2.setBusinessFlag(localCursor.getString(12));
				localCity2.setCityCode(localCursor.getString(1));
				localCity2.setCityId(localCursor.getString(0));
				localCity2.setCityName(localCursor.getString(5));
				localCity2.setFirstPy(localCursor.getString(6));
				localCity2.setAreaCode(localCursor.getString(9));
				localCity2.setProvinceCode(localCursor.getString(2));
				localCity2.setFirstLetter(str);
				this.citylist.add(localCity2);
				localCursor.moveToNext();
			}
		}
	}

	public void addHotCityFirstLetter() {
		if (this.mCityFirstLetter.length <= 0)
			return;
		String str;
		do {
			str = this.mCityFirstLetter[0];
		} while (this.mHotCursor == null);
		if (this.mHotCursor.moveToNext()) {
			City localCity1 = new City();
			localCity1.setCityName("-1");
			localCity1.setFirstLetter(str);
			this.citylist.add(localCity1);
		}
		while (true) {
			if (this.mHotCursor.isAfterLast()) {
				this.mHotCursor.close();
				break;
			}
			City localCity2 = new City();
			localCity2.setBusinessFlag(this.mHotCursor.getString(12));
			localCity2.setCityCode(this.mHotCursor.getString(1));
			localCity2.setCityId(this.mHotCursor.getString(0));
			localCity2.setCityName(this.mHotCursor.getString(5));
			localCity2.setFirstPy(this.mHotCursor.getString(6));
			localCity2.setAreaCode(this.mHotCursor.getString(9));
			localCity2.setProvinceCode(this.mHotCursor.getString(2));
			localCity2.setFirstLetter(str);
			this.citylist.add(localCity2);
			this.mHotCursor.moveToNext();
		}
	}

	public void initAutoData() {
		ArrayList localArrayList = new ArrayList();
		for (int i = this.mHotCount;; i++) {
			if (i >= this.citylist.size()) {
				this.mAutoAdapter = new ArrayAdapter(this, R.id.selected_item,
						R.id.city_info, localArrayList);
				return;
			}
			City localCity = (City) this.citylist.get(i);
			if ((localCity.getFirstPy() == null)
					|| (localCity.getCityName() == null))
				continue;
			localArrayList.add(localCity.getCityName());
			localArrayList.add(localCity.getFirstPy() + ","
					+ localCity.getCityName());
		}
	}

	public void initDBHelper() {
		this.mAutoTextView = ((EditText) findViewById(R.id.change_city_auto_text));
		this.mDB = NativeDBHelper.getInstance(this);
		this.mHotCursor = this.mDB.select(null,
				"IS_FREQUENT = '1' ORDER BY CAST(SORT_VALUE AS INTEGER)", null);
	}

	public void setAdapter() {
		this.mListView = ((ListView) findViewById(R.id.city_list));
		this.mListAdapter = new CityListAdapter(this, this.citylist);
		this.mListView.setAdapter(this.mListAdapter);
		
		this.mAutoListView = ((ListView) findViewById(R.id.auto_city_list));
		this.mAutoCityAdapter = new AutoCityAdapter(this);
		this.mAutoListView.setAdapter(this.mAutoCityAdapter);
		this.mAutoListView.setVisibility(View.GONE);
	}

	public void setAutoCompassTextViewOnItemClickListener() {
		mAutoListView.setOnItemClickListener(cityAutoTvOnClickListener);
//		this.mAutoTextView.setOnItemClickListener(cityAutoTvOnClickListener);
	}

	public void setFirstletter() {
		this.mCityFirstLetter = getResources().getStringArray(
				R.array.city_first_letter);
	}

	public void setListViewOnItemClickListener() {
		this.mListView.setOnItemClickListener(cityLvOnClickListener);
	}

	public void setTheContentView() {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.select_city);
	}

	private AdapterView.OnItemClickListener cityLvOnClickListener = new AdapterView.OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View v, int position,
				long id) {
			ListView listView = (ListView) parent;
			City localCity = (City) listView.getItemAtPosition(position);
			if (!"-1".equals(localCity.getCityName())) {
//				Toast.makeText(SelectCityActivity.this,
//						"你选择的城市是" + localCity.getCityName(), Toast.LENGTH_SHORT)
//						.show();
				selectComplete(localCity);
			}
		}
	};

	private AdapterView.OnItemClickListener cityAutoTvOnClickListener = new AdapterView.OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View v, int position,
				long id) {
			String str1 = (String) mAutoCityAdapter.getItem(position);
			if (str1.contains(",")) {
				for (String str2 = str1.split(",")[1];; str2 = str1) {
					String[] arrayOfString = new String[2];
					arrayOfString[0] = "2";
					arrayOfString[1] = str2.trim();
					Cursor localCursor = mDB.select(null, "TYPE=? AND SHORT_NAME=?",
							arrayOfString);
					if (localCursor.moveToFirst()) {
						City localCity = new City();
						localCity.setAreaCode(localCursor.getString(9));
						localCity.setCityCode(localCursor.getString(1));
						localCity.setCityId(localCursor.getString(0));
						localCity.setCityName(localCursor.getString(5));
						localCity.setProvinceCode(localCursor.getString(2));
						selectComplete(localCity);
					}
					return;
				}
			}
		}
	};
	
	protected void onPause() {
		super.onPause();
		//this.imm.hideSoftInputFromInputMethod(this.mAutoTextView.getWindowToken(), 0);
	}

	protected void onResume() {
		super.onResume();
		//this.imm.hideSoftInputFromInputMethod(this.mAutoTextView.getWindowToken(), 0);
	}
	
	public void onScrollStateChanged(AbsListView paramAbsListView, int paramInt) {
	}

}
